package org.itboys.seller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.itboys.commons.CommonConstants;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mongodb.utils.page.Page;
import org.itboys.mongodb.utils.query.QueryParamUtils;
import org.itboys.seller.entity.SellerMenu;
import org.itboys.seller.service.SellerMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

/**
 * 商家菜单
 * @author weisky
 *
 */
@RestController 
@RequestMapping("/seller/menu") 
public class SellerMenuController extends BaseController{

	@Autowired
	private SellerMenuService sellerMenuService;
	
	/**
	 * 跳转到商家菜单页面
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/toListVM")
	public ModelAndView toListVM(HttpServletRequest request, HttpServletResponse response) {
		return new ModelAndView("/seller/menu-list");
	}
	
	@RequestMapping("/list")
	public void list(HttpServletRequest request, HttpServletResponse response){
		Map<String, Object> param = QueryParamUtils.builderQueryMap(request);
		Page<SellerMenu> page = sellerMenuService.pageQuery(param, request);
		JsonPageUtils.renderJsonPage(page.getTotal(), page.getData(), response);
	}
	
	/**
	 * 保存或者修改
	 * @param sellerMenu
	 * @param request
	 * @param response
	 */
	@RequestMapping("/save")
	public void save(SellerMenu sellerMenu,
			HttpServletRequest request, HttpServletResponse response){
		boolean result = sellerMenuService.insert(sellerMenu);
		if(result){
			AjaxUtils.renderText(response, CommonConstants.SUCCESS);
		}else{
			AjaxUtils.renderText(response, CommonConstants.FAIL);
		}
	}
	
	@RequestMapping("/getById")
	public void getById(Long sellerMenuId, HttpServletResponse response){
		AjaxUtils.renderJson(response, sellerMenuService.getById(sellerMenuId));
	}
	
	/**
	 * 删除
	 * @param sellerMenuId
	 * @param response
	 */
	@RequestMapping("/delete")
	public void delete(Long sellerMenuId, HttpServletResponse response){
		boolean result = sellerMenuService.dele(sellerMenuId);
		if(result){
			AjaxUtils.renderText(response, CommonConstants.SUCCESS);
		}else{
			AjaxUtils.renderText(response, CommonConstants.FAIL);
		}
	}
}
